const EmcConfig = {
    getProperties(dataId) {
        const url = new URL(window.location.origin + '/runtime/emc-config?group=DEFAULT_GROUP&dataId=' + dataId);

         return new Promise((resolve, reject) => {
             GM_xmlhttpRequest({
                 method: 'GET',
                 url: url.toString(),
                 cookie: document.cookie,
                 onload: function (response) {
                     if (response.status >= 200 && response.status < 300) {
                         try {
                             const data = response.responseText;
                             resolve(data);
                         } catch (e) {
                             console.error('解析响应失败:', e);
                         }
                     } else {
                         console.error('请求失败:', response.status, response.statusText);
                     }
                 },
                 onerror: function (error) {
                     console.error('请求出错:', error);
                 }
             });
         });
    },
    _createToken(kid, k) {
        const url = new URL('http://10.20.183.93:50100/jwt/getvalue?kid=' + kid + '&k=' + k);
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: 'GET',
                url: url.toString(),
                onload: function (response) {
                    if (response.status >= 200 && response.status < 300) {
                        try {
                            const data = response.responseText;
                            resolve(data);
                        } catch (e) {
                            console.error('解析响应失败:', e);
                        }
                    } else {
                        console.error('请求失败:', response.status, response.statusText);
                    }
                },
                onerror: function (error) {
                    console.error('请求出错:', error);
                }
            });
        });
    },
    extractKeyValuesFromHTML(htmlString) {
        // 1. 使用 DOMParser 解析 HTML 字符串
        const parser = new DOMParser();
        const doc = parser.parseFromString(htmlString, 'text/html');

        // 2. 查找 <pre><code> 内容
        const codeBlock = doc.querySelector('pre code.code-block');
        if (!codeBlock) return {};

        // 3. 提取 key/value
        const lines = codeBlock.textContent.trim().split('\n');
        return lines.reduce((result, line) => {
            const [key, value] = line.split('=').map(part => part.trim());
            if (key && value) result[key] = value;
            return result;
        }, {});
    },
    async getMyAuthSecret() {
        const htmlString = await EmcConfig.getProperties('global.properties');
        const globalUrlConfig =  EmcConfig.extractKeyValuesFromHTML(htmlString);
        const appSecretStr = globalUrlConfig.appSecret;
        return JSON.parse(appSecretStr);
    },
    async getMyAuthToken(secretKid, secretK) {
        return await EmcConfig._createToken(secretKid, secretK);
    },
    async getGloablUrl() {
        const htmlString = await EmcConfig.getProperties('global_url.properties');
        return EmcConfig.extractKeyValuesFromHTML(htmlString);
    }
};